Testing User-defined Functions

Common things to test How to fix bugs

Testing is an often overlooked but critical step in programming user-defined functions. Many functions are fairly straightforward implementations of formulas you are given. However, it is important to test each function that you define to ensure that it produces the correct result before you use it to solve a problem.

If your function calculates a value like the FtoC function we defined in a previous lesson than you can test its correctness on some known values. We know that 212degee symbolF is equal to 100degee symbolC and that 32degee symbolF=0degee symbolC. So we can call (use) our FtoC function as follows from the Command Window and ensure that the results are what we expect.

FtoC(212)
FtoC(32)
FtoC(70)

We could also try a couple of values that are very low, 15degee symbolF, -10degee symbolF, and some that are high 250degee symbolF and use a thermometer to check the accuracy within some range we are interested in. If we are developing the formula for such a conversion, we would want to test several different known boundary conditions. However, we chose a well-known temperature conversion for our example and testing it on a few values should be enough to convince you that you have programmed your function correctly.

In later lessons, we will use selection and repetition statements to implement more complex algorithms in our functions. You will learn to test for and fix common bugs (errors) in complex code. For now, be sure to test each function that you define before using it as part of a larger solution to a problem.